//
//  ContactHelper.m
//  ShandongNews
//
//  Created by yanyan on 14-7-22.
//  Copyright (c) 2014年 zengyuan. All rights reserved.
//

#import "ContactHelper.h"
#import "RegexKitLite.h"

extern "C"
{
#import "Allpinyin.h"
#import "pinyin.h"
}

@implementation ContactHelper

+ (void)initPinyin
{
    NSString *indexFilePath = [[NSBundle mainBundle] pathForResource:@"pinyin_index" ofType:@"txt"];
    NSString *pinyinFilePath = [[NSBundle mainBundle] pathForResource:@"pinyin" ofType:@"txt"];
    init_pinyin([indexFilePath UTF8String],[pinyinFilePath UTF8String]);

}


+ (NSString *)getFullPinyinString:(NSString *)str
{
    NSString *sTmp = @"";
    for (int i = 0; i < [str length]; i++)
    {
        unichar c = [str characterAtIndex:i];
        NSString *s = [NSString stringWithFormat:@"%C",c];
        if ([s isMatchedByRegex:@"[\u4e00-\u9fa5]"])
        {
            const char *str = hanzi2pinyin(c);
            s = [NSString stringWithUTF8String:str];
        }
        sTmp = [sTmp stringByAppendingString:s];
    }
    return [sTmp lowercaseString];
    
}

 + (NSString *)getFirstLetter:(NSString *)str
{
    if (!str || [str isEqualToString:@""])
    {
        return @"#";
    }
    NSString *theFirstLetter = [str substringToIndex:1];

    NSString* rule1 = @"[a-zA-Z]";
    NSPredicate* pred=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",rule1];
    BOOL isMatch=[pred evaluateWithObject:theFirstLetter];
    
    if (isMatch)
    {
        return [theFirstLetter uppercaseString];
    }
    return @"#";
}

//+(void)makePin:(AWUserInfo *)userinf{
//    
//    //获取全拼
//    userinf.fullpinyin = [ContactHelper getFullPinyinString:userinf.name];
//    //得到首字母
//    userinf.firstLetter = [ContactHelper getFirstLetter:userinf.fullpinyin];
//
//}



@end
